<?php
namespace app\common\traits\model;

use think\Validate;
use Carbon\Carbon;

/**
 * 用户操作信息
 */
trait UserOperateCheck
{
    protected $operate_count = 6;
    protected $operate_expire = 21600;

    /**
     * 设置时间范围内最多操作次数
     * @param integer $num [description]
     */
    public function setOperateMaxCount($num = 6)
    {
        $this->operate_count = (int) $num;
        return $this;
    }
    /**
     * 设置失效时间间隔
     * @param integer $expire [description]
     */
    public function setOperateExpire($expire = 21600)
    {
        $this->operate_expire = (int) $expire;
        return $this;
    }
    /**
     * 获取最大操作次数
     * @return [type] [description]
     */
    private function getOperateMaxCount()
    {
        return $this->operate_count;
    }
    /**
     * 获取失效时间
     * @return [type] [description]
     */
    private function getOperateExpire()
    {
        return $this->operate_expire;
    }
    /**
     * 获取设定时间间隔用户操作次数
     * @param  [type] $name [description]
     * @param  string $type [description]
     * @return [type]       [description]
     */
    private function getOperateCount($name, $type = '')
    {
        if (!$this->checkOperateExpire($name, $type, $this->getOperateExpire())) {
            $this->initOperateInfo($name, $type);
        }
        return session($name.'.'.$type.'Count') ? session($name.'.'.$type.'Count') : 0;
    }
    /**
     * 初始化用户操作信息次数
     * @param  [type] $name [description]
     * @param  [type] $type [description]
     * @return [type]       [description]
     */
    private function initOperateCount($name, $type = '')
    {
        session($name.'.'.$type.'Count', 0);
    }
    /**
     * 初始化用户操作信息数组
     * @param  [type] $name [description]
     * @param  string $type [description]
     * @return [type]       [description]
     */
    private function initOperateInfo($name, $type = '')
    {
        $this->initOperateCount($name, $type);
        $this->initUserOperateStartTime($name, $type);
    }
    /**
     * 增加操作次数
     * @param [type] $name [description]
     * @param [type] $type [description]
     */
    public function addOperateCount($name, $type = '')
    {
        $i = 1;
        session($name.'.'.$type.'Createtime') ? : $this->initUserOperateStartTime($name, $type);
        if ($c = $this->getOperateCount($name, $type)) {
            $i = ++$c;
        }
        $this->setOperateCount($name, $type, $i);
    }
    /**
     * 设置操作次数
     * @param [type] $name [description]
     * @param [type] $type [description]
     * @param [type] $i    [description]
     */
    private function setOperateCount($name, $type = '', $i)
    {
        session($name.'.'.$type.'Count', $i);
    }
    /**
     * 校验操作次数
     * @param  [type] $name [description]
     * @param  [type] $type [description]
     * @return [type]       [description]
     */
    public function checkOperateCount($name, $type = '')
    {
        return $this->getOperateCount($name, $type) < $this->getOperateMaxCount() ? true : false;
    }
    /**
     * 初始化 操作时间
     * @param  [type] $name [description]
     * @param  string $type [description]
     * @return [type]       [description]
     */
    public function initUserOperateStartTime($name, $type = '')
    {
        session($name.'.'.$type.'Createtime', Carbon::now()->getTimestamp());
    }
    /**
     * 获取上次初始化到当前的时间间隔
     * @param  [type] $name [description]
     * @param  string $type [description]
     * @return [type]       [description]
     */
    private function getUserOperateExpire($name, $type = '')
    {
        return session($name.'.'.$type.'Createtime') ? Carbon::now()->diffInSeconds(Carbon::createFromTimestamp(session($name.'.'.$type.'Createtime'))) : 0;
    }
    /**
     * 检验上次初始化后到当前时间是否失效
     * @param  [type] $name [description]
     * @param  string $type [description]
     * @return [type]       [description]
     */
    public function checkOperateExpire($name, $type = '', $expire = 21600)
    {
        return $this->getUserOperateExpire($name, $type) > $expire ? false : true;
    }
}
